From bb2f79e09dd4776d611e4751ede1cbb43340fba0 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 16 Dec 2023 17:31:12 +0100 Subject: fix(build): handle Next.js errors and warnings during build * extract Blog component from BlogPage (paginated) and extract Article component from ArticlePage to avoid `Cannot read properties` errors due to fallback route * fix sitemap build (cjs not supported) * fix eslint warnings (react/jsx-no-literals) * update `start` script since I'm using standalone output * update `postbuild` script since we need to copy public and static files to standalone directory (Next.js does not handle it itself because we should use a CDN...) --- src/pages/blog/page/[number].tsx | 44 +++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'src/pages/blog/page/[number].tsx') diff --git a/src/pages/blog/page/[number].tsx b/src/pages/blog/page/[number].tsx index fa1123d..e524a64 100644 --- a/src/pages/blog/page/[number].tsx +++ b/src/pages/blog/page/[number].tsx @@ -3,7 +3,7 @@ import type { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; -import { useCallback } from 'react'; +import { type FC, useCallback } from 'react'; import { useIntl } from 'react-intl'; import { getLayout, @@ -78,23 +78,12 @@ type BlogPageProps = { translation: Messages; }; -/** - * Blog index page. - */ -const BlogPage: NextPageWithLayout = ({ +const Blog: FC> = ({ data, lastCursor, pageNumber, }) => { - useRedirection({ - isReplacing: true, - to: ROUTES.BLOG, - whenPathMatches: (path) => - path === `${ROUTES.BLOG}${PAGINATED_ROUTE_PREFIX}/1`, - }); - const intl = useIntl(); - const { isFallback } = useRouter(); const { articles, error, @@ -256,8 +245,6 @@ const BlogPage: NextPageWithLayout = ({ [intl] ); - if (isFallback) return ; - const pageUrl = `${CONFIG.url}${ROUTES.BLOG}`; return ( @@ -359,6 +346,30 @@ const BlogPage: NextPageWithLayout = ({ ); }; +/** + * Blog index page. + */ +const BlogPage: NextPageWithLayout = ({ + data, + lastCursor, + pageNumber, +}) => { + useRedirection({ + isReplacing: true, + to: ROUTES.BLOG, + whenPathMatches: (path) => + path === `${ROUTES.BLOG}${PAGINATED_ROUTE_PREFIX}/1`, + }); + + const { isFallback } = useRouter(); + + return isFallback ? ( + + ) : ( + + ); +}; + BlogPage.getLayout = (page) => getLayout(page); type BlogPageParams = { @@ -414,7 +425,8 @@ export const getStaticPaths: GetStaticPaths = async () => { { length: totalPages }, (_, index) => index + 1 ); - const paths = pagesArray.map((number) => { + // We remove /blog/page/1 since it is redirected to /blog + const paths = pagesArray.slice(1).map((number) => { return { params: { number: `${number}` } }; }); -- cgit v1.2.3